require("../../assets/css/index.less");
require("./myhome.less");
require("../../assets/fonts/iconfont.css");
let https = require("../../utils/https.js");
let dom = require("../../utils/dom.js");
let local = require("../../utils/local.js");
// 是否登录
// dom.isLogin();
//--------------------轮播效果------------------------------
//导入swiper
import Swiper from "swiper/bundle";
import "swiper/css/bundle";
// import "weui";
require("../../assets/lib/weui/weui.css");
let weui = require("../../assets/lib/weui/weui.js");

dom.ready(function () {
  dom.jump("#jump", "new-home");
  dom.jump("#sportspic", "sports-data");
  dom.jump("#badge-jump", "mybadge");
  // init Swiper:
  const swiper = new Swiper(".swiper", {
    loop: true,
    autoplay: {
      delay: 2000,
      stopOnLastSlide: false,
      disableOnInteraction: false,
    },
    // If we need pagination
    pagination: {
      el: ".swiper-pagination",
    },

    // Navigation arrows
    navigation: {
      nextEl: ".swiper-button-next",
      prevEl: ".swiper-button-prev",
    },
  });
  // /获取个人信息功能
  function getUserInfo() {
    //发送ajax获取个人信息
    https.get("/api/user/info", (res) => {
      //判定
      if (res.errno == 0) {
        //解构数据
        let { imgUrl, nickName, sign, clockCount, duration, calorie } =
          res.data;
        //1.渲染数据到页面的指定位置
        dom.getEle("#avatarPic").src = https.BASE_URL + imgUrl;
        dom.getEle("#nickTxt").innerHTML = nickName;
        dom.getEle("#signTxt").innerHTML = sign;
        dom.$("#clockText").innerHTML = clockCount;
        dom.$("#km").innerHTML = duration + `<span class="f14">分钟</span>`;
        dom.$("#kc").innerHTML = calorie + `<span class="f14">千卡</span>`;
        //2.把最新用户信息存储到本地
        local.set("user", res.data);
      }
    });
  }
  // 打卡
  function getClockInfo() {
    https.get("/api/user/clockInInfo", (res) => {
      if (res.errno == 0) {
        console.log(res.data.isClockIn, "+++++++");
        dom.$("#clockbtn").innerHTML = res.data.isClockIn
          ? "已打卡"
          : "今日打卡";
        dom.$("#clockText").innerHTML = res.data.clockCount;
      }
    });
  }
  getClockInfo();

  dom.addEvent("#clockbtn", function () {
    getClockInfo();
    https.post("/api/user/clockIn", {}, (res) => {
      if (res.errno == 0) {
        alert("今日已打卡");
        getClockInfo();
        getInfo();
      } else {
        alert(res.message);
      }
    });
  });
  //当页面加载时直接调用
  getUserInfo();

  // 页脚
  dom.createFoot(3);
});
